package com.yupi.springbootinit.mq.DirectExchange;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;

public class DirectProducer {
    //1:定义交换机的名称
    public static final String EXCHANGE_NAME = "direct_exchange";

    public static void main(String[] args) throws IOException, TimeoutException {
        //2：创建连接工厂，指定信息
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.1.183");
        factory.setUsername("lfc");
        factory.setPassword("123456");
        Connection connection = factory.newConnection();
        //4：创建频道
        Channel channel = connection.createChannel();
        //5：声明交换机，名称以及类型
        channel.exchangeDeclare(EXCHANGE_NAME,"direct");
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String string = scanner.nextLine();
            String[] splits = string.split("|");
            String routingKey = splits[0];
            String message = splits[2];
            //6：绑定交换机，向哪个交换机发送
            channel.basicPublish(EXCHANGE_NAME,routingKey,null,message.getBytes("UTF-8"));
            System.out.println(routingKey+"  Sent '" + message + "'");
        }
    }
}
